<?php

class Application_Model_ModuloPerfilAcesso extends Zend_Db_Table_Abstract {

    protected $_name = 'modulo_pa';
    protected $_primary = array('modulo','perfil_acesso');
    protected $_dependentTables = array('');
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_ModuloPerfilAcesso();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insere($data,$codigo){
        
        if(is_array($data) && count($data) > 0){
            foreach ($data as $value) {
                $value['perfil_acesso'] = $codigo;
                parent::insert($value);
            }
            
        }
    }
    
    public function update($data,$usuario){
        $this->removeByPessoa($usuario);
        
        foreach ($data as $value) {
            $obj['plano_saude'] = $value;
            $obj['pessoa'] = $usuario;
            parent::insert($obj);
        }
        
    }
    
    public function getByPerfilAcesso($perfil) {
        $select = $this->getAdapter()->select();
        $select->from(
                    array('m' => $this->_name),
                    array(  'm.perfil_acesso','m.modulo','m.edita','m.consulta'
                    ),
                    $this->_schema
                )
                ->where('m.perfil_acesso = \''.$perfil.'\'');
        
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function getByModuloAcesso($pessoa) {
        $select = $this->getAdapter()->select();
        $select->from(
                    array('pa' => $this->_name),
                    array( 'm.chave as modulo','pf.descricao as perfil_acesso', 'pa.edita','pa.consulta'
                    ),
                    $this->_schema
                )
                ->joinLeft(array('m' => 'modulo'), 'm.codigo = pa.modulo',array(''))
                ->joinLeft(array('pf' => 'perfil_acesso'), 'pf.codigo = pa.perfil_acesso',array(''))
                ->joinLeft(array('f' => 'funcionario'), 'f.perfil_acesso = pf.codigo',array(''))
                ->where("f.codigo = ".$pessoa);
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function getByPessoa($id){
        return $this->fetchAll("pessoa = " . $id);
    }
    
    public function removeByPerfilAcesso($id){
        parent::delete("perfil_acesso =" . $id);
    }
 
    
    public function getMenu($perfil){
        $select = $this->getAdapter()->select()
            ->from( array('pa' => $this->_name),
                    array( 'm.chave','pa.edita','pa.consulta','m.nome'),
                    $this->_schema
                  )
                ->join(array('m' => 'modulo'), 'm.codigo = pa.modulo',array(''))
                ->where("pa.perfil_acesso = ".$perfil);
        return $this->getAdapter()->fetchAll($select);
    }
}